//
//  ImageEditViewController.m
//  Uhomebk
//
//  Created by Chiang on 2017/10/24.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ImageEditView.h"
//#import "UIImage+Color.h"

@interface ImageEditView ()

@property(nonatomic,assign)CGPoint lastPoint;
@property(nonatomic,assign)BOOL isSwiping;
@property(nonatomic,assign)CGFloat red;
@property(nonatomic,assign)CGFloat green;
@property(nonatomic,assign)CGFloat blue;
@property(nonatomic,strong)NSMutableArray * xPoints;
@property(nonatomic,strong)NSMutableArray * yPoints;
@property(nonatomic,strong)UIImageView * imageView;
@property(nonatomic, strong) UIBezierPath *bezier;
//存储Undo出来的线条
@property(nonatomic, strong) NSMutableArray *cancleArray;

@end

@implementation ImageEditView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor blackColor];
        [self seg_layout];
    }
    return self;
}

- (void)seg_layout {
    self.imageView=[[UIImageView alloc]initWithFrame:CGRectZero];
    [self addSubview:self.imageView];
    
    UIView *bottomBarView = [self newBgView];
    [self addSubview:bottomBarView];
    [bottomBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self);
        make.height.mas_equalTo(49);
    }];
    
    UILabel *decLabel = [[UILabel alloc] init];
    decLabel.font = kFont_11;
    decLabel.textColor = kColor_W;
    decLabel.text = @"画圈编辑问题点";
    [bottomBarView addSubview:decLabel];
    [decLabel sizeToFit];
    [decLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(bottomBarView.mas_centerY);
        make.left.equalTo(bottomBarView.mas_left).offset(15);
    }];
    
    UIButton *doneButton = [[UIButton alloc] init];
    [doneButton setBackgroundImage:[UIImage imageWithColor:kColor_T] forState:UIControlStateNormal];
    [doneButton setBackgroundImage:[UIImage imageWithColor:kColor_T_Press] forState:UIControlStateNormal];
    [doneButton addTarget:self action:@selector(doneButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [doneButton setTitle:@"完成" forState:UIControlStateNormal];
    [doneButton setTitleColor:kColor_W forState:UIControlStateNormal];
    [doneButton.titleLabel setFont:kFont_14];
    doneButton.layer.cornerRadius = 2;
    doneButton.layer.masksToBounds = YES;
    [bottomBarView addSubview:doneButton];
    [doneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(bottomBarView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(60, 30));
        make.right.equalTo(bottomBarView.mas_right).offset(-10);
    }];
    
    UIView *topBarView = [self newBgView];
    [self addSubview:topBarView];
    [topBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self);
        make.height.mas_equalTo(64);
    }];
    
  
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = kFont_16;
    titleLabel.textColor = kColor_W;
    titleLabel.text = @"编辑图片";
    [topBarView addSubview:titleLabel];
    [titleLabel sizeToFit];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(topBarView.mas_centerX);
        make.centerY.equalTo(topBarView.mas_centerY).offset(10);
    }];
    
    UIImage *image = [UIImage imageWithContentsOfFile:SEGPicName(@"navBackNor")];
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    [leftButton setBackgroundImage:image forState:UIControlStateNormal];
    [leftButton setBackgroundImage:[UIImage imageWithContentsOfFile:SEGPicName(@"navBackPre")] forState:UIControlStateHighlighted];
    [leftButton addTarget:self action:@selector(onNavigationLeftButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [topBarView addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topBarView.mas_left);
        make.centerY.equalTo(topBarView.mas_centerY).offset(10);
    }];
}

- (UIView *)newBgView {
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor blackColor];
    bgView.alpha = 0.8;
    return bgView;
}

-(void)setEditImage:(UIImage *)editImage {
    self.imageView.image = editImage;
    float scale = LYScreenWidth()/editImage.size.width;
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.centerY.equalTo(self);
        make.height.mas_equalTo(editImage.size.height *scale);
    }];
}

- (void)onNavigationLeftButtonClicked:(UIButton *)sender {
    [self removeFromSuperview];
}

- (void)doneButtonClicked:(UIButton *)sender {
    [self showImage:self.imageView.image];
    [self removeFromSuperview];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.isSwiping    = false;
    UITouch * touch = touches.anyObject;
    self.lastPoint = [touch locationInView:self.imageView];
    [self.xPoints addObject:[NSNumber numberWithFloat:self.lastPoint.x]];
    [self.yPoints addObject:[NSNumber numberWithFloat:self.lastPoint.y]];
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.isSwiping = true;
    UITouch * touch = touches.anyObject;
    CGPoint currentPoint = [touch locationInView:self.imageView];
    UIGraphicsBeginImageContext(self.imageView.frame.size);
    [self.imageView.image drawInRect:(CGRectMake(0, 0, self.imageView.frame.size.width, self.imageView.frame.size.height))];
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), self.lastPoint.x, self.lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextSetLineCap(UIGraphicsGetCurrentContext(),kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),255 / 255.0f, 88 / 255.0f, 88 / 255.0f, 1.0);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.lastPoint = currentPoint;
    [self.xPoints addObject:[NSNumber numberWithFloat:self.lastPoint.x]];
    [self.yPoints addObject:[NSNumber numberWithFloat:self.lastPoint.y]];
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if(!self.isSwiping) {
        // This is a single touch, draw a point
        UIGraphicsBeginImageContext(self.imageView.frame.size);
        [self.imageView.image drawInRect:(CGRectMake(0, 0, self.imageView.frame.size.width, self.imageView.frame.size.height))];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),255 / 255.0f, 88 / 255.0f, 88 / 255.0f, 1.0);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), self.lastPoint.x, self.lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), self.lastPoint.x, self.lastPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
}

#pragma mark getter && setter
-(NSMutableArray*)xPoints
{
    if (!_xPoints) {
        _xPoints=[[NSMutableArray alloc]initWithCapacity:0];
    }
    return _xPoints;
}
-(NSMutableArray*)yPoints
{
    if (!_yPoints) {
        _yPoints=[[NSMutableArray alloc]initWithCapacity:0];
    }
    return _yPoints;
}

#pragma mark delegate
- (void)showImage:(UIImage *)image
{
    //检测代理有没有实现代理方法
    if(self.delegate && [self.delegate respondsToSelector:@selector(editFinish:)])
    {
        [self.delegate editFinish:image];
    }else{
        NSLog(@"代理没有实现changeStatus:方法");
    }
}
@end
